

import javax.swing.JOptionPane;
//import com.mysql.jdbc.Driver;
import java.sql.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Miriam
 */
public class BaseDatos {
    //ATRIBUTOS
      private Connection conexion;
      private String nombre, user, psw, url;
    
         
    public BaseDatos()
    {      
        nombre = "tienda";
        user = "root";
        psw = "ramponce";
        url = "localhost";
        estableceConexion();
        System.out.println("Conexión establecida");        
    }
    
     public  Connection estableceConexion() {
         
          try {
               Class.forName("com.mysql.jdbc.Driver");
               String connectionUrl2 = "jdbc:mysql://localhost/mysql?" +
                                      "user=root";
               
                String connectionUrl = "jdbc:mysql://"+url+"/mysql?" +
                                      "user=" + user + "&password=" + psw;
               
               conexion = DriverManager.getConnection(connectionUrl);
           } catch (SQLException e) {
               System.out.println("SQL Exception: "+ e.toString());
            } catch (ClassNotFoundException cE) {
               System.out.println("Class Not Found Exception: "+ cE.toString());
           }
         return conexion;     
     }

    
     public ResultSet consulta(String query)
     {
         ResultSet tabla = null;
         try
         {
            // /Crear objeto Statement para realizar queries a la base de datos
            Statement instruccion = conexion.createStatement();

            //Un objeto ResultSet, almacena los datos de resultados de una consulta
            tabla = instruccion.executeQuery(query);

           
            System.out.println("tamaño " + tabla.getFetchSize());
            /*while(tabla.next())
            {
                
                System.out.println(tabla.getString(1));
                System.out.println(tabla.getString(2));
                System.out.println(tabla.getString(3));
            }*/
            
         } catch (SQLException e) {
               System.out.println("SQL Exception: "+ e.toString());
                    
         }
         return tabla;
     }
     
     public void cerrarConexion()
     {
         try
         {
            conexion.close();
            System.out.println("Conexion cerrada exitosamente");
         }catch(SQLException e)
         {
             JOptionPane.showMessageDialog(null,"ERROR AL CERRAR CONEXION");
         }
     }
}
